package com.leps.nq.bu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leps.nq.bu.entity.BuArea;
import com.leps.nq.bu.mapper.*;
import com.leps.nq.bu.service.IBuAreaService;
import com.leps.nq.bu.vo.AreaQueryModel;
import com.leps.nq.bu.vo.AreaVo;
import com.leps.nq.common.util.SecurityUtil;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.op.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 区块表 服务实现类
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@Service
public class BuAreaServiceImpl extends ServiceImpl<BuAreaMapper, BuArea> implements IBuAreaService {
    @Autowired
    private BuAreaMapper buAreaMapper;
    @Autowired
    private BuStationMapper buStationMapper;
    @Autowired
    private BuPlcMapper buPlcMapper;
    @Autowired
    private BuPumpMapper buPumpMapper;
    @Autowired
    private BuWellMapper buWellMapper;
    @Autowired
    private OpReplenishMapper replenishMapper;
    @Autowired
    private OpAlarmMapper alarmMapper;
    @Autowired
    private OpInspectionMapper inspectionMapper;
    @Autowired
    private OpRetireMapper retireMapper;
    @Autowired
    private OpWaterTestMapper waterTestMapper;
    @Autowired
    private OpBubbleReplenishMapper bubbleReplenishMapper;
    @Autowired
    private OpDeviceMaintenanceMapper deviceMaintenanceMapper;
    @Autowired
    private BuUserAreaMapper userAreaMapper;
    @Autowired
    private BuUserCustMapper userCustMapper;

    @Override
    public PageVo<AreaVo> pageByQueryModel(AreaQueryModel queryModel) {
        Page<AreaVo> page = Page.of(queryModel.getPageNum(), queryModel.getPageSize());
        page = this.buAreaMapper.selelctByQueryModel(page, queryModel);
        return PageVo.of(page);
    }

    @Override
    @Transactional
    public boolean updateCascade(BuArea area) {
        final int areaId = area.getAreaId();
        final String areaName = area.getAreaName();
        final String origin = this.buAreaMapper.selectById(areaId).getAreaName();
        // 补填数据
        area.setUpdateBy(SecurityUtil.getUserName());
        area.setUpdateTime(new Date());
        // 更新区块
        this.buAreaMapper.updateById(area);
        this.buAreaMapper.updateRedundantField(area);
        // 如有必要，更新子表的区块名称
        if (Objects.equals(origin, areaName)) return true;
        this.buStationMapper.updateAreaName(areaName, areaId);
        this.buPlcMapper.updateAreaName(areaName, areaId);
        this.buPumpMapper.updateAreaName(areaName, areaId);
        this.buWellMapper.updateAreaName(areaName, areaId);
        this.alarmMapper.updateProperty("area_name", areaName, "area_id", areaId);
        this.bubbleReplenishMapper.updateProperty("area_name", areaName, "area_id", areaId);
        this.inspectionMapper.updateProperty("area_name", areaName, "area_id", areaId);
        this.replenishMapper.updateProperty("area_name", areaName, "area_id", areaId);
        this.retireMapper.updateProperty("area_name", areaName, "area_id", areaId);
        this.waterTestMapper.updateProperty("area_name", areaName, "area_id", areaId);
        return true;
    }

    @Override
    public boolean save(BuArea entity) {
        super.save(entity);
        // 分配该区块权限给上级客户管理员
        this.userAreaMapper.insertBatchFromUserCustRelation(entity);
        return true;
    }

    @Override
    @Transactional
    public boolean saveAndFillRedundantField(BuArea area) {
        area.setCreateBy(SecurityUtil.getUserName());
        area.setCreateTime(new Date());
        this.buAreaMapper.insert(area);
        this.buAreaMapper.updateRedundantField(area);
        // 分配该区块权限给上级客户管理员
        this.userAreaMapper.insertBatchFromUserCustRelation(area);
        return true;
    }

    @Override
    public List<BuArea> listForSelect(Integer custId) {
        if(SecurityUtil.isAdmin()) {
            return this.buAreaMapper.selectList(new QueryWrapper<BuArea>().eq("cust_id", custId));
        }
        long userId = SecurityUtil.getUserId();
        return this.buAreaMapper.selectListByCustIdAndUserId(custId, userId);
    }
}
